package com.smart.manager.task;


import cn.hutool.core.collection.CollUtil;
import com.smart.common.annotation.TaskBean;
import com.smart.common.annotation.TaskMethod;
import com.smart.common.entities.domain.TaskResult;
import com.smart.manager.member.entities.models.MemberMaterialPig;
import com.smart.manager.member.entities.models.MemberProfileAccount;
import com.smart.manager.member.entities.models.query.QMemberMaterialPig;
import com.smart.manager.member.entities.models.query.QMemberProfileAccount;
import com.smart.manager.member.entities.views.MemberView;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 模拟任务 有参和无参任务模拟
 *
 * @author Administrator
 * @date 2022/09/16
 */
@Log4j2
@Component
@TaskBean
@RequiredArgsConstructor
public class ChickenAddTask {

    @TaskMethod(title = "更新偷蛋次数")
    public synchronized TaskResult chickenAddTask() {
        try {


        List<MemberProfileAccount> accountList = new QMemberProfileAccount().findList();
        List<MemberMaterialPig> materialPigs = new QMemberMaterialPig().findList();
        Set<Long> collect = materialPigs.stream().map(x->x.getMember().getId()).collect(Collectors.toSet());
        accountList.forEach(x->{
            if (!collect.contains(x.getMember().getId())){
                //不包含等于普通用户
                x.setChickenQuantity(3);
            }else {
                //
                long count = materialPigs.stream().filter(y -> {
                    return y.getMember().getId().equals(x.getMember().getId()) && y.getIsFull();
                }).count();
                if (count>0){
                    x.setChickenQuantity(8); //高级用户
                }else {
                    x.setChickenQuantity(5); //普通买猪用户
                }
            }
        });
        DB.updateAll(accountList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new TaskResult(true, "成功!");
    }


}
